package main

import (
	"fmt"
	"os"

	"github.com/sniperHW/kendynet"
	"github.com/tealeg/xlsx"
)

func main() {
	xlFile, err := xlsx.OpenFile("../Type.xlsx")
	if err != nil {
		panic(err)
	}

	sheet := xlFile.Sheets[0]
	rows := sheet.Rows[3:]

	str :=
		`//所有项目的类型枚举
package com_type

const(
`

	for _, row := range rows {
		name := row.Cells[1].String()
		type_ := row.Cells[2].String()
		name_type := fmt.Sprintf("%s_%s", name, type_)
		enum, _ := row.Cells[3].Int()
		desc := row.Cells[5].String()
		str = str + fmt.Sprintf("	%s = %d //%s\n", name_type, enum, desc)
	}
	str = str + ")\n"

	out_path := "../../../../common/com_type/com_type.go"

	f, err := os.OpenFile(out_path, os.O_RDWR, os.ModePerm)
	if err != nil {
		if os.IsNotExist(err) {
			f, err = os.Create(out_path)
			if err != nil {
				kendynet.Errorf("create %s failed:%s", out_path, err.Error())
				return
			}
		} else {
			kendynet.Errorf("open %s failed:%s", out_path, err.Error())
			return
		}
	}

	err = os.Truncate(out_path, 0)

	_, err = f.WriteString(str)

	if nil != err {
		kendynet.Errorf("%s Write error:%s\n", out_path, err.Error())
	} else {
		kendynet.Infof("%s Write ok\n", out_path)
	}

	f.Close()
}
